package com.yanqu.dao;

import com.yanqu.data.entity.setting.GoodsIgnoreData;
import com.yanqu.jdbc.JdbcTemplateHelper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GoodsIgnoreDataDaoImpl extends TempDataObjectDao<GoodsIgnoreData> {

    public long getMaxId() {
        String sql = "select ifnull(max(dbId),0) as maxId from t_s_monitor_goods_white ";
        long maxId = 0;
        try {
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(sql);
            if(sqlRowSet.next()) {
                maxId = sqlRowSet.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return maxId;
    }

    public static String getGoodsIgnoreDataSql = "select * from `t_s_monitor_goods_white`";
    public Map<Integer, Map<Integer, GoodsIgnoreData>> getGoodsIgnoreData() {
        Map<Integer, Map<Integer, GoodsIgnoreData>> result = new ConcurrentHashMap<>();
        try {
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(getGoodsIgnoreDataSql);
            while (sqlRowSet.next()) {
                GoodsIgnoreData data = parse(sqlRowSet);
                if(!result.containsKey(data.getChannelId())){
                    result.put(data.getChannelId(), new ConcurrentHashMap<>());
                }
                result.get(data.getChannelId()).put(data.getGoodsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

        }
        return result;
    }

    public static String updateGoodsIgnoreDataSql = "update `t_s_monitor_goods_white` set channelId=?,goodsId=?,deleted=? where dbId=?";
    @Override
    protected boolean updateData(GoodsIgnoreData data) {
        data.setBeginWriteTime();
        int[] types = new int[]{Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.BIGINT};
        Object[] values = new Object[4];
        int i = 0;
        values[i++] = data.getChannelId();
        values[i++] = data.getGoodsId();
        values[i++] = data.getDeleted();
        values[i++] = data.getDbId();
        if (JdbcTemplateHelper.getJdbcTemplate().update(updateGoodsIgnoreDataSql, values, types) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGoodsIgnoreDataSql = "insert into `t_s_monitor_goods_white`(dbId,channelId,goodsId,deleted) values (?,?,?,?)";
    @Override
    protected boolean addData(GoodsIgnoreData data) {
        data.setBeginWriteTime();
        int[] types = new int[]{Types.BIGINT, Types.INTEGER, Types.INTEGER, Types.INTEGER};
        Object[] values = new Object[4];
        int i = 0;
        values[i++] = data.getDbId();
        values[i++] = data.getChannelId();
        values[i++] = data.getGoodsId();
        values[i++] = data.getDeleted();
        if (JdbcTemplateHelper.getJdbcTemplate().update(insertGoodsIgnoreDataSql, values, types) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GoodsIgnoreData parseData(SqlRowSet rs) throws SQLException {
        GoodsIgnoreData goodsIgnoreData = new GoodsIgnoreData();
        goodsIgnoreData.setDbId(rs.getLong("dbId"));
        goodsIgnoreData.setChannelId(rs.getInt("channelId"));
        goodsIgnoreData.setGoodsId(rs.getInt("goodsId"));
        goodsIgnoreData.setDeleted(rs.getInt("deleted"));

        return goodsIgnoreData;
    }

}
